package com.sdhs.file.utils;

import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.sdhs.file.config.OssConfig;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class OssTemplate {

	@Autowired
	private OssConfig ossConfig;
	
	/**
	 * 简单上传
	 * @param bucketName
	 * @param fileName
	 * @param multipartFile
	 * @return
	 */
	public String putObjectSimple(String bucketName, String fileName, MultipartFile multipartFile) {
		OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
		InputStream inputStream = null;
		String url = null;
		try {
			inputStream = multipartFile.getInputStream();
			ossClient.putObject(bucketName, fileName, inputStream);
			String[] endpointArray = ossConfig.getEndpoint().split("//");
            url = endpointArray[0] + "//" + bucketName + "." + endpointArray[1] + "/" + fileName;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
            if (inputStream != null) {
            	IOUtils.closeQuietly(inputStream);
            }
        }
		return url;
	}
	
	/**
     * 文件下载
     *
     * @param bucketName   文件夹
     * @param fileName     文件名称
     * @param originalName 下载的文件名
     * @param response     response
     * @return
     */
	public void downloadFile(String bucketName, String fileName, String originalName, String suffix, HttpServletResponse response) {
		OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
        InputStream inputStream = null;
        try {
            OSSObject ossObject = ossClient.getObject(bucketName, fileName);
            inputStream = ossObject.getObjectContent();
            if (StringUtils.isNotEmpty(originalName)) {
                fileName = originalName;
            }
            response.reset();
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(originalName, "UTF-8"));
            response.setCharacterEncoding(StandardCharsets.UTF_8.name());
            String fileContent = FileContentType.contentType(suffix);
            response.setContentType(fileContent);
            FileUtil.writeFile(inputStream, response.getOutputStream());
        } catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
            if (inputStream != null) {
            	IOUtils.closeQuietly(inputStream);
            }
        }
    }
	
}





